package yanyan.com.tochar.utils;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;

/**
 * Created by wuyr on 17-11-14 下午4:22.
 */

@SuppressWarnings({"unused", "WeakerAccess"})
public class BitmapPixelUtil {
    public static Bitmap getBlackAndWhiteImg(Bitmap bitmap){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        Bitmap res=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        int alpha = 0xFF << 24;
        for(int x=0;x<width;x++){
            for(int y=0;y<height;y++){
                int pixel = bitmap.getPixel(x, y);
                int red = (pixel & 0xff0000) >> 16;
                int green = (pixel & 0x00ff00) >> 8;
                int blue = (pixel & 0x0000ff);
                int gray = (int) (red *  0.3 + green * 0.59 + blue * 0.11);
                gray = alpha | (gray << 16) | (gray << 8) | gray;
                res.setPixel(x,y,gray);
            }
        }
        return res;

    }
    public static Bitmap generateHiddenImg(Bitmap showBitmap,Bitmap hiddenBitmap){
        int b_width = hiddenBitmap.getWidth();
        int b_height = hiddenBitmap.getHeight();
        int w_width = showBitmap.getWidth();
        int w_height = showBitmap.getHeight();

        int f_width=Math.max(b_width,w_width);
        int f_height=Math.max(w_height,b_height);
        Bitmap res=Bitmap.createBitmap(f_width,f_height,Bitmap.Config.ARGB_8888);

        //黑色图片距离边缘的距离
        int b_widthOffset = b_width == f_width ? 0 : (f_width - b_width) / 2;
        int b_heightOffset = b_height == f_height ? 0 : (f_height - b_height) / 2;
        //白色图片离边缘距离
        int w_widthOffset = w_width == f_width ? 0 : (f_width - w_width) / 2;
        int w_heightOffset = w_height == f_height ? 0 : (f_height - w_height) / 2;


        for(int x=0;x<f_width;x++){
            for(int y=0;y<f_height;y++){
                //上下左右交叉排列黑白像素
                boolean blackPixel = (x + y) % 2 == 0 ? true : false;

                int coor_x;
                int coor_y;
                //决定当前像素位置是否对应图一或图二某像素，如果没有，跳过循环
                boolean validPixel = true;
                if (!blackPixel) {
                    coor_x = x - b_widthOffset;
                    if (coor_x > b_width - 1) validPixel = false;
                    coor_y = y - b_heightOffset;
                    if (coor_y > b_height - 1) validPixel = false;
                }else {
                    coor_x = x - w_widthOffset;
                    if (coor_x > w_width - 1) validPixel = false;
                    coor_y = y - w_heightOffset;
                    if (coor_y > w_height - 1) validPixel = false;
                }

                validPixel = validPixel && coor_x >= 0 && coor_y >= 0;
                if (!validPixel) continue;

                //根据颜色计算像素灰度，设定透明度
                if (blackPixel) {
                    int pixel = showBitmap.getPixel(coor_x, coor_y);
                    int red = (pixel & 0xff0000) >> 16;
                    int green = (pixel & 0x00ff00) >> 8;
                    int blue = (pixel & 0x0000ff);
                    int gray = (red * 19595 + green * 38469 + blue * 7472) >> 16;
                    int argb = Color.argb(255 - gray, 0, 0, 0);
                    res.setPixel(x,y,argb);
                }else {
                    int pixel = hiddenBitmap.getPixel(coor_x, coor_y);
                    int red = (pixel & 0xff0000) >> 16;
                    int green = (pixel & 0x00ff00) >> 8;
                    int blue = (pixel & 0x0000ff);
                    int gray = (red * 19595 + green * 38469 + blue * 7472) >> 16;
                    int argb = Color.argb( gray, 255, 255, 255);
                    res.setPixel(x,y,argb);
                }

            }
        }

        return res;
    }

}
